package com.mazaiting.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mazaiting.manager.domain.entity.SysRolePermission;
import com.mazaiting.manager.mapper.ISysRolePermissionMapper;
import com.mazaiting.manager.service.ISysRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色关联的权限 服务实现类
 * </p>
 *
 * @author mazaiting
 * @since 2022-09-12
 */
@Slf4j
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<ISysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

    @Override
    public List<Long> listPermIdsByRoleId(Long roleId) {
        return baseMapper.listPermIdsByRoleId(roleId);
    }

    @Override
    public boolean deleteByRoleId(Long roleId) {
        // 如果未查询到数据则返回成功
        List<Long> idList = lambdaQuery().select(SysRolePermission::getId).eq(SysRolePermission::getRoleId, roleId).list().stream().mapToLong(SysRolePermission::getId).boxed().toList();
        if (CollectionUtil.isEmpty(idList)) return true;
        // 批量删除
        return removeBatchByIds(idList);
    }

    @Override
    public boolean save(Long roleId, List<Long> permIds) {
        if (CollectionUtil.isEmpty(permIds)) return true;
        return saveBatch(permIds.stream().map(permId -> new SysRolePermission(roleId, permId)).collect(Collectors.toList()));
    }

    @Override
    public List<String> listBtnPermByRoles(List<String> roles) {
        return baseMapper.listBtnPermByRoles(roles);
    }
    
}
